/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package utility.typeSafe;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultComboBoxModel;
import utility.NameValueList;

/**
 *
 * @author jay
 */
public class EnumUtils {
    
    public static <T extends Enum<T>> List<NameValueList> enumToNameValueList(Class<T> enums) {
        try {
            List<NameValueList> nameValueList = new ArrayList<>();
            Method getName = enums.getMethod("getName");
            for (Object e : enums.getEnumConstants()) {
                nameValueList.add(new NameValueList(e.toString(), (String) getName.invoke(e)));
            }
            return nameValueList;
        } catch (Exception ex) { throw new RuntimeException(ex); }
    }
    
    public static <T extends Enum<T>> DefaultComboBoxModel getEnumDefaultComboBoxModel(Class<T> enums) {
        try {
            List<NameValueList> nameValueList = enumToNameValueList(enums);
            DefaultComboBoxModel defaultComboBoxModel = new DefaultComboBoxModel();
            
            for (NameValueList e : nameValueList) {
                defaultComboBoxModel.addElement(e);
            }
            return defaultComboBoxModel;
        } catch (Exception ex) { throw new RuntimeException(ex); }
    }
    
}
